'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 奖励相关
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var rewardType = require('../consts/rewardType');
var moduleType = require('../consts/moduleType');
var operationType = require('../consts/operationType');
var roleService = require('./roleService');
var soldierConf = require('../config/soldierConf');

// todo: 增加所需装入数据表检测
// 给用户添加多个奖励
exports.addRewardListToRole = function (player_manager, reward_list, module_type, operation_type, res_int_val, res_str_val) {
    module_type = module_type || moduleType.MDL_COMMON_REWARD;
    operation_type = operation_type || operationType.OPR_CMN_REWARD;
    var result = [];
    _.each(reward_list, function (reward_item) {
        result.push(_addRewardToRole(player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val));
    });
    return result;
};

/**
 * 给用户添加一个奖励
 */
exports.addRewardToRole = function (player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val) {
    module_type = module_type || moduleType.MDL_COMMON_REWARD;
    operation_type = operation_type || operationType.OPR_CMN_REWARD;
    return _addRewardToRole(player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val);
};

// jshint maxstatements:80
var _addRewardToRole = function (player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val) {
    var reward_type = reward_item.reward_type;
    var reward = reward_item.reward;
    var reward_num = reward_item.reward_num || 1;
    switch (reward_type) {
    case rewardType.GOLD:
        {
            roleService.addGoldLimit(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.DIAMOND:
        {
            roleService.addDiamond(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.PROPERTY:
        {
            player_manager.bag.addProperty(reward, reward_num);
            break;
        }
    case rewardType.CONSUME:
        {
            player_manager.bag.addConsume(reward, reward_num);
            break;
        }
    case rewardType.EQUIP:
        {
            var hole_num = reward_item.hole_num || 0;
            var lv = reward_item.reward_value || 0;
            for (var i3 = 0; i3 < reward_num; i3++) {
                player_manager.bag.addEquipWithAttrAndHole(
                    reward,
                    hole_num,
                    lv
                );
            }
            break;
        }
    case rewardType.EXP:
        {
            roleService.addExp(player_manager, reward_num);
            break;
        }
    case rewardType.DRAGON_GOLD:
        {
            roleService.addDragonGold(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.SOLDIER_SOUL:
        {
            roleService.addSoldierSoul(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.MAGIC:
        {
            roleService.addMagic(player_manager, reward_num);
            break;
        }
    case rewardType.FOOD:
        {
            roleService.addFoodLimit(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.PROTECT:
        {
            roleService.addProtectTime(player_manager, reward_num);
            break;
        }
    case rewardType.ENERGY:
        {
            roleService.addEnergy(player_manager, module_type, operation_type, reward_num, res_int_val, res_str_val);
            break;
        }
    case rewardType.VIP_EXP:
        {
            var role_model = player_manager.role;
            var old_vip_lv = role_model.getVip();
            role_model.addVipExp(reward_num);
            var new_vip_lv = role_model.getVip();
            if (new_vip_lv > old_vip_lv) {
                if (!roleService.vipChanged(player_manager, old_vip_lv, new_vip_lv)) {
                    console.warn('发放VIP经验奖励出错，module_type=%j, operation_type=%j', module_type, operation_type);
                }
            }
            break;
        }
    case rewardType.HONOR:
        {
            player_manager.role.addHonor(reward_num);
            break;
        }
    case rewardType.PROSPEROUS:
        {
            player_manager.role.addprosperous(reward_num);
            break;
        }
    case rewardType.ARENA_COIN:
        {
            player_manager.arena.addArenaCoin(reward_num);
            break;
        }
    case rewardType.EXPEDITION_COIN:
        {
            player_manager.expedition.addExpeditionCoin(reward_num);
            break;
        }
    case rewardType.SOLDIER:
        {
            reward_item = _rewardSoldier(player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val);
            break;
        }
    default:
        {
            console.error('reward type is wrong:' + reward_type);
            break;
        }
    }

    return reward_item;
};

var _rewardSoldier = function (player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val) {
    var soldier_model = player_manager.soldier;
    var soldier_type = reward_item.reward;

    var soldier_type_conf = soldierConf.getSoldierTypeConf(soldier_type);
    var soldier_item = soldier_model.getSoldier(soldier_type);
    if (!soldier_item) {
        soldier_model.addSoldier(soldier_type, soldier_type_conf.start_star);
        return reward_item;
    }

    reward_item.reward_type = rewardType.PROPERTY;
    reward_item.reward = soldier_type_conf.patch_id;
    reward_item.reward_num = soldier_type_conf.change_patch;

    return exports.addRewardToRole(player_manager, reward_item, module_type, operation_type, res_int_val, res_str_val);
};
